package org.lalib.controller.action;

import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CVariableForm;
import org.lalib.controller.form.CVariableForm.VariableDefination;
import org.lalib.controller.service.CCategoryService;
import org.lalib.controller.service.CVariableService;
import org.lalib.entity.object.ECategory;
import org.lalib.entity.object.variable.DropDownListVariable;
import org.lalib.entity.object.variable.EVariable;
import org.lalib.entity.object.variable.EnumVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller(value = "cVariableAction")
public class CVariableAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private CVariableService cVariableService;

	@Autowired
	private CCategoryService cCategoryService;

	@Autowired
	private CVariableForm cVariableForm;

	public String init(ECategory category) {
		List<ECategory> categoryList = cCategoryService.getAllParentCategoriesForGivenCategory(category);
		Map<String, String> variableMap = cVariableService.getVariableMap();
		List<SelectItem> variableSelectItemList = cVariableService.getVariableSelectItemListFromVariableMap(variableMap);
		getVariablesForGivenCategoryList(categoryList, category);
		cVariableForm.setVariableTypeMap(variableMap);
		cVariableForm.setVariableTypeSelectItemList(variableSelectItemList);
		cVariableForm.setCategory(category);
		cVariableForm.setSubCategoryList(categoryList);
		return "variableDefination";
	}

	private void getVariablesForGivenCategoryList(List<ECategory> categoryList, ECategory category) {
		List<EVariable> variablesList = cVariableService.getAllVariablesForGivenCategoryList(categoryList);
		List<EVariable> variables = cVariableService.getVariablesForGivenCategory(category);
		cVariableForm.setVariableListOfParentCategories(variablesList);
		cVariableForm.setVariableDefination(cVariableForm.new VariableDefination());
		cVariableForm.setVariableListOfSelectedCategory(variables);
		cVariableForm.setCategory(category);
	}

	public void save() {
		VariableDefination variableDefination = cVariableForm.getVariableDefination();
		ECategory category = cVariableForm.getCategory();
		cVariableService.saveNewVariable(variableDefination, category);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), category);
	}

	public void showDeletePanel(EVariable variable) {
		cVariableForm.setVariable(variable);
	}

	public void showEnumPanel(EVariable variable) {
		cVariableForm.setEnumVariable((EnumVariable) variable);
	}

	public void showDropPanel(EVariable eVariable) {
		cVariableForm.setDropVariable((DropDownListVariable) eVariable);
	}

	public void saveEnumVariable() {
		EnumVariable enumVariable = cVariableForm.getEnumVariable();
		EVariable variable = cVariableService.getVariableById(enumVariable.getId());
		enumVariable = (EnumVariable) variable;
		enumVariable.addChoice(cVariableForm.getEnumKey(), cVariableForm.getEnumValue());
		cVariableService.update(enumVariable);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), cVariableForm.getCategory());
		cVariableForm.setEnumVariable(enumVariable);
		cVariableForm.setEnumKey("");
		cVariableForm.setEnumValue("");
	}

	public void saveDropVariable() {
		DropDownListVariable dropVariable = cVariableForm.getDropVariable();
		EVariable variable = cVariableService.getVariableById(dropVariable.getId());
		dropVariable = (DropDownListVariable) variable;
		dropVariable.addChoice(cVariableForm.getDropKey(), cVariableForm.getDropValue());
		cVariableService.update(dropVariable);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), cVariableForm.getCategory());
		cVariableForm.setDropVariable(dropVariable);
		cVariableForm.setDropKey("");
		cVariableForm.setDropValue("");
	}

	public void removeDropKey(String key) {
		DropDownListVariable downListVariable = cVariableForm.getDropVariable();
		EVariable variable = cVariableService.getVariableById(downListVariable.getId());
		downListVariable = (DropDownListVariable) variable;
		downListVariable.removeChoice(key);
		cVariableService.update(downListVariable);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), cVariableForm.getCategory());
		cVariableForm.setDropVariable(downListVariable);
	}

	public void delete() {
		EVariable variable = cVariableForm.getVariable();
		cVariableService.delete(variable);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), cVariableForm.getCategory());
	}

	public void removeEnumKey(String key) {
		EnumVariable enumVariable = cVariableForm.getEnumVariable();
		EVariable variable = cVariableService.getVariableById(enumVariable.getId());
		enumVariable = (EnumVariable) variable;
		enumVariable.removeChoice(key);
		cVariableService.update(enumVariable);
		getVariablesForGivenCategoryList(cVariableForm.getSubCategoryList(), cVariableForm.getCategory());
		cVariableForm.setEnumVariable(enumVariable);
	}
}